﻿#ifndef SCRIPTINSTRUCTION_H
#define SCRIPTINSTRUCTION_H

#include <qobject.h>

#include <qhash.h>

#define __SCRIPTINSTRUCTION_REGISTER(className,type,fun) int __scriptinstruction_register_##className=ScriptInstruction::insertToFromByteArrayMap(type,fun);

/*
ScriptInstruction类为脚本指令的具体实现类提供接口和某些公共成员变量的管理。
所有脚本指令都应该继承该类。
*/
class ScriptInstruction :public QObject
{
  Q_OBJECT
public:
  //枚举类Instruction为全部指令类别提供编号，如果
  enum ScriptInstructionFlag {
    MetaInstruction_Wait = 1,//操作：等待
    //MetaInstruction_Goto,//操作：跳转
    //MetaInstruction_If,//操作：判断
    MetaInstruction_Loop,//操作开始一个循环
    Instruction_DataReader_StartCollection,//打开监听器
    Instruction_DataReader_StopCollection,//关闭监听器
    Instruction_TurnTable_Connect,//连接
    Instruction_TurnTable_Disconnect,//断开连接
    Instruction_TurnTable_SetLoad,//设置负载
    Instruction_TurnTable_PowerOn,//上电
    Instruction_TurnTable_PowerOff,//下电
    Instruction_TurnTable_CloseCircuit,//闭合
    Instruction_TurnTable_OpenCircuit,//闲置
    Instruction_TurnTable_Stop,//停止
    Instruction_TurnTable_EmergencyStop,//急停
    Instruction_TurnTable_SearchZero,//寻零
    Instruction_TurnTable_MoveToZero,//回零
    Instruction_TurnTable_PositionMode,//位置方式参数
    Instruction_TurnTable_RateMode,//速率方式参数
    Instruction_TurnTable_Run,//运行
    Instruction_TurnTable_CurrentState,//查询现场数据
    Instruction_TurnTable_SearchZeroState,//查询寻零状态
    Instruction_TurnTable_RunningState,//查询运行状态
    Instruction_TurnTable_RunningMode,//查询运动方式
    Instruction_Thermostat_Connect,//打开温箱连接
    Instruction_Thermostat_Disconnect,//断开温箱连接
    Instruction_Thermostat_TargetTemperature,//设置温箱目标温度
    Instruction_Thermostat_CurrentTemperature,//查询温箱当前温度
    Instruction_Thermostat_TemperatureRate,//设置变温速率
    Instruction_TurnTable_QuitConsole//退出转台控制台
  };
protected://property
  static QHash<uchar,ScriptInstruction*(*)(char*const,int,int*)> *fromByteArrayMap;//代理函数映射protected:
  uchar instructionFlag;
  uint deviceId=0;
private:
  static const int byteArraySize=1;
public:
  explicit ScriptInstruction();
  ScriptInstruction(ScriptInstruction const & scriptInstruction);
  ScriptInstruction & operator=(ScriptInstruction const & scriptInstruction);
  virtual ~ScriptInstruction();
public:
  virtual QString toString()=0;//得到描述
  virtual QByteArray toByteArray();//转二进制
  virtual ScriptInstruction* clone()=0;//克隆函数
  static ScriptInstruction* fromByteArray(QByteArray data);//从二进制得到
  static ScriptInstruction* fromByteArray(char * const src,int size,int * offset=Q_NULLPTR);//从二进制得到
public:
  static int insertToFromByteArrayMap(uchar type,ScriptInstruction*(*fun)(char*const,int,int*));
public://geter and seter
  uchar getInstructionFlag();
  ScriptInstruction* setInstructionFlag(uchar instructionFlag);
  uint getDeviceId();
  ScriptInstruction* setDeviceId(uint deviceId);
};

#endif
